package com.chuangjiangx.member.stored.ddd.query;

import com.chuangjiangx.domain.shared.model.Enable;
import com.chuangjiangx.member.stored.ddd.dal.mapper.RechargeRuleDalMapper;
import com.chuangjiangx.member.stored.ddd.dal.dto.CustomRecharges;
import com.chuangjiangx.member.stored.ddd.dal.dto.RechargeRuleById;
import com.chuangjiangx.member.stored.ddd.dal.dto.RechargeRuleList;
import com.chuangjiangx.member.stored.ddd.query.dto.CustomRechargeDTO;
import com.chuangjiangx.member.stored.ddd.query.dto.RechargeRuleByIdDTO;
import com.chuangjiangx.member.stored.ddd.query.dto.RechargeRuleListForMerchantDTO;
import com.chuangjiangx.member.basic.ddd.domain.model.CustomRecharge;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;


/**
 * 储值规则
 *
 * @author leeXyan
 */
@Component
public class MbrRechargeRuleQuery {


    @Autowired
    private RechargeRuleDalMapper rechargeRuleDalMapper;

    /**
     * 根据规则id查询储值规则
     *
     * @param rechargeRuleId
     * @return RechargeRuleByIdDTO
     */
    public RechargeRuleByIdDTO queryById(Long rechargeRuleId) {

        Assert.notNull(rechargeRuleId, "商户id不能为空");

        RechargeRuleById rechargeRuleById = rechargeRuleDalMapper.queryById(rechargeRuleId);
        if (rechargeRuleById != null) {
            RechargeRuleByIdDTO rechargeRuleByIdDTO = new RechargeRuleByIdDTO();
            BeanUtils.copyProperties(rechargeRuleById, rechargeRuleByIdDTO);
            return rechargeRuleByIdDTO;
        }
        return null;
    }


    /**
     * 储值规则列表查询-客户端，H5
     *
     * @param merchantId 所属商户id
     * @return list
     */
    public List<RechargeRuleList> queryRulesList(Long merchantId) {
        Assert.notNull(merchantId, "商户id不能为空！");
        //需要过滤掉赠送类型是卡券并且卡券过期的记录
        List<RechargeRuleList> rechargeRuleLists = rechargeRuleDalMapper.queryRulesList(merchantId);

        return rechargeRuleLists;
    }

    /**
     * 储值规则列表查询-商户平台
     *
     * @param merchantId 所属商户id
     * @return
     */
    public List<RechargeRuleListForMerchantDTO> queryRulesListForMerchant(Long merchantId) {
        Assert.notNull(merchantId, "商户id不能为空！");
        List<RechargeRuleList> rechargeRuleLists = rechargeRuleDalMapper.queryRulesListForMerchant(merchantId);
        List<RechargeRuleListForMerchantDTO> list = new ArrayList<>();
        rechargeRuleLists.forEach(rechargeRuleList -> {

            //设置储值规则属性值
            RechargeRuleListForMerchantDTO dto = new RechargeRuleListForMerchantDTO();
            BeanUtils.copyProperties(rechargeRuleList,dto);
            if (rechargeRuleList.getEnable() != null) {
                dto.setEnableText(Enable.getEnable(rechargeRuleList.getEnable()).name);
            }
            list.add(dto);
        });

        return list;
    }

    /**
     * 获取自定义储值开关状态
     */
    public CustomRechargeDTO checkRulesOpen(Long merchantId) {

        CustomRecharges customRecharges = rechargeRuleDalMapper.checkCustomRulesOpen(merchantId);
        CustomRechargeDTO dto = new CustomRechargeDTO();

        if (customRecharges == null) {
            dto.setCustomRecharge(CustomRecharge.CLOSE.value);
            dto.setCustomRechargeText(CustomRecharge.CLOSE.name);
        } else {
            BeanUtils.copyProperties(customRecharges, dto);
            if (customRecharges.getCustomRecharge() == 0) {
                dto.setCustomRechargeText(CustomRecharge.CLOSE.name);
            } else {
                dto.setCustomRechargeText(CustomRecharge.OPEND.name);
            }
        }
        return dto;
    }




}
